<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增目录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <style type="text/css">
        .redstar{
            color: red;
            margin-right: 5px;
        }
        .layui-field-title{
            color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" lay-filter="modelInfo" action="" style="margin:10px 20px 0 10px">
            <input type="text" id="id" name="id" class="layui-input" style="display: none">
            <input type="text" id="repId" name="repId" class="layui-input" style="display: none">
            <input type="text" id="pid" name="pid" class="layui-input" style="display: none">
            <div class="layui-form-item" style="display: none">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>编号</label>
                <div class="layui-input-block">
                    <input type="text" id="code" name="code" lay-verify="require" autocomplete="off" placeholder="请输入编号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>名称</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="name" name="name" lay-verify="required" autocomplete="off" placeholder="请输入名称">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="yes" class="layui-btn yes" lay-submit lay-filter="submit" style="display: none">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>


<script src="../layui/layui.js" charset="utf-8"></script>
<script src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="../js/common.js"></script>
<script src="../lib/dist/xm-select.js"></script>

<script>
    layui.use(['form','element','table'],function () {
        var element = layui.element,
            form = layui.form,
            table = layui.table;

        /**
         * 获取初始数据
         */
        var id = getUrlParams('id'),
            modelId = getUrlParams('modelId'),
            parentId = getUrlParams('pid'),
            code = getUrlParams('code'),
            name = getUrlParams('name'),
            repId=getUrlParams('repId');
            modelTreeArr = []

        $(document).ready(function () {
            if (id !== null && id !== undefined) {
                $.ajax({
                    type: "get",
                    url: QUALITY_URL + '/dataCollection/dicategory/findDiCateryById.do?id='+id,
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    success: function (redata) {
                        console.log(redata.result)
                        form.val('modelInfo', {
                            'id': redata.result.id,
                            'pid':redata.result.pid,
                            'name': redata.result.name,
                            'repId' : redata.result.repId
                        });
                    },
                    error: function (redata) {
                    }
                });



            }else{
                form.val('modelInfo',{
                    'pid':parentId,
                    'repId' : repId
                });
            }
        });


        window.addEventListener('message', function (event) {
            if (event.data === 'submit') {
                $("#yes").trigger("click");
            }
        });


        /**
         * 监听提交
         */
        form.on('submit(submit)', function (data) {
            if (data.field.id != null && data.field.id !== '') {

                /**
                 * 修改目录信息
                 */
                $.ajax({
                    type: "post",
                    url: "/dataCollection/dicategory/update.do",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('update_success-' + redata.message, '*');
                        } else if (redata.code === '9999') {
                            layer.msg(redata.message,{icon:2});
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('update_error', '*');
                    }
                });
            } else {
                var entity = data.field
                $.ajax({
                    type: "post",
                    url: "/dataCollection/dicategory/add.do",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('success-' + redata.message, '*');
                        } else if (redata.code === '9999') {
                            layer.alert(redata.message, function(index) {
                                layer.close(index);
                            });
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('error', '*');
                    }
                });
            }
            return false;
        });
       



        function getListModelById(id) {
            $.ajax({
                type: "get",
                url: QUALITY_URL + '/dataCollection/dicategory/findDiCateryById.do?id='+id,
                dataType: 'json',
                cache: false,
                contentType: "application/json",
                success: function (redata) {

                },
                error: function (redata) {
                }
            });
        }
    });

    window.getUrlParams = function (key) {
        //获取url参数
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var url = decodeURI(window.location.search)
        var r = url.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
</script>

</body>
</html>